<?php
namespace app\admin\controller;
use app\admin\model\Position as PositionModel;
use app\admin\controller\Base;

class Position extends Base
{
    
    public function lst()
    {
        $list=PositionModel::paginate(20);


        $this->assign('list',$list);

        $position=db('position')->select();
       // print_r($position);
        $this->assign('position',$position);
        $area=db('area')->select();

        $this->assign('area',$area);
        return view();
    }

    public function add()
    {
        if (request()->isPost())
        {
            $data=[
                'pname'=>input('name'),
                'level'=>input('level')
            ];
            $validate = \think\Loader::validate('Position');
            if (!$validate->scene('add')->check($data))
            {
                $this->error($validate->getError());die;
            }
            if (db('position')->insert($data))
        {
            return $this->success('部门添加成功','lst');
        }
        else
        {
            return $this->error('部门添加失败','lst');
        }

            return ;
        }
        return $this->fetch();
    }
    
    public function edit()
    {
        $id=input('id');
        $position=db('position')->find($id);
        if (request()->isPost())
        {
            $data=[
                'pname'=>input('name'),
                'level'=>input('level')
            ];

            $validate = \think\Loader::validate('Position');
            if (!$validate->scene('edit')->check($data))
            {
                $this->error($validate->getError());die;
            }
            $save=db('position')->where('id','=',$id)->update($data);
            if ($save!==false)
            {
                $this->success('修改部门成功','lst');
            }
            else {
                $this->error('修改部门失败');
            }
        }
        
        
        $this->assign('position',$position);
        return $this->fetch();
    }
    
    public function del()
    {
        $id=input('id');
            if (db('position')->delete(input('id'))){
                $this->success('部门删除成功','lst');
            }
            else {
                $this->error('部门删除失败','lst');
            }
        }

    public function ajaxEdit(){
        $input = input();
        $res = db('position')->find($input['eid']);
        return $this->returnjson(200,$res);
    }

    public function ajaxseave()
    {
        $input=input();
        $save=db('position')->where('id','=',$input['id'])->update(['pname'=>$input['name'],'area_id'=>$input['area_id'],'level'=>$input['level']]);
        if($save){
            return ['ECODE'=>200,'MSG'=>'修改成功'];
        }
    }

    public function ajaxadd()
    {
        $input=input();
        $save=db('position')->insert($input);
        if($save){
            return ['ECODE'=>200,'MSG'=>'添加成功'];
        }
    }



    //Ajax异步修改管理员状态
    public function changestatus(){
        if(request()->isAjax()){
            $positionid=input('positionid');
            $pstatus=db('position')->field('pstatus')->where('id',$positionid)->find();
            $pstatus=$pstatus['pstatus'];
            if($pstatus==1){
                db('position')->where('id',$positionid)->update(['pstatus'=>0]);
                echo 1;//由显示改为隐藏
            }
            elseif($pstatus==0){
                db('position')->where('id',$positionid)->update(['pstatus'=>1]);
                echo 2;//由隐藏改为显示
            }
        }
        else{
            $this->error("非法操作");
        }

    }


}
